package com.systop.scos.notice.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.common.modules.fileattch.model.FileAttch;
import com.systop.common.modules.fileattch.service.FileAttchManager;
import com.systop.common.modules.security.user.LoginUserService;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.ExtJsCrudAction;
import com.systop.scos.notice.model.Notice;
import com.systop.scos.notice.service.NoticeManager;

/**
 * 通知管理的Action。
 * 
 * @author ShangHua
 */
@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class NoticeAction extends ExtJsCrudAction<Notice, NoticeManager> {

	/**
	 * 登陆用户信息管理
	 */
	@Autowired
	private LoginUserService loginUserService;

	/**
	 * 附件管理
	 */
	@Autowired
	private FileAttchManager fileAttchManager;

	/**
	 * 查询起始时间
	 */
	private Date startTime;

	/**
	 * 查询截至时间
	 */
	private Date endTime;

	/**
	 * 按通知名称查询通知
	 */
	@Override
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer sql = new StringBuffer("from Notice n where 1=1 ");
		List args = new ArrayList();
		if (StringUtils.isNotBlank(getModel().getTitle())) {
			sql.append(" and n.title like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getTitle()));
		}
		if (startTime != null && endTime != null) {
			sql.append(" and n.createTime >= ? and n.createTime <= ? ");
			args.add(startTime);
			args.add(endTime);
		}
		sql.append(" order by n.createTime desc");
		page = getManager().pageQuery(page, sql.toString(), args.toArray());
		restorePageData(page);
		return INDEX;
	}

	/**
	 * 保存通知
	 */
	@Override
	public String save() {
		User user = loginUserService.getLoginUser(getRequest());
		if (user == null) {
			addActionError("未登录，请登录后访问本页面。");
			return INPUT;
		}
		getModel().setPublisher(user);
		getModel().setCreateTime(new Date());
		getManager().getDao().clear();
		getManager().save(getModel());
		return SUCCESS;
	}

	/**
	 * 查看通知内容
	 */
	public String view() {
		return "view";
	}

	/**
	 * 删除且级联删除各部门通知附件
	 */
	@Override
	public String remove() {
		if (getModel().getId() != null) {
			Notice notice = getManager().get(getModel().getId());
			if (StringUtils.isNotBlank(notice.getAttachIds())) {
				// 更新附件表的Type为通知公告表的附件
				String[] test = notice.getAttachIds().split(",");
				for (int i = 0; i < test.length; i++) {
					FileAttch fileAttch = fileAttchManager.getFileAttch(Integer
							.valueOf(test[i]));
					if (fileAttch != null) {
						fileAttchManager.remove(fileAttch);
					}
				}
			}
			getManager().remove(notice);
		}
		return SUCCESS;
	}

	/**
	 * 
	 * @return
	 */
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
}
